package com.xframe.common.core.service.command;

import com.xframe.common.config.CommandRunTimeArgs;
import com.xframe.common.core.constant.ErrorConstant;
import com.xframe.domain.CommandModel;
import com.xframe.common.core.dto.ErrorCode;
import com.xframe.common.core.exception.BusinessException;
import com.xframe.domain.ModelRepositoryFactory;
import com.xframe.domain.repository.SysBaseRepository;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.event.bus.core.model.DataMessage;
import com.xframe.event.bus.core.msg.MessageSender;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.IModel;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.util.StringUtil;
import java.lang.reflect.Method;

/**
 * @Description: 删除单个模型
 * @ClassName: DeleteModelProc
 * @Author: xufeng
 * @CreateTime: 2023/4/6
 **/
public class    DeleteModelProc extends BaseCommand{
    @Override
    public Object ProcCommand(CommandModel requestArgs, IUserCredentials userCredentials) throws BusinessException {
        try {
            if (StringUtil.isEmpty(requestArgs.getModelCode())) {
                throw new BusinessException(ErrorCode.MODELID_NULL, ErrorConstant.MODELID_NULL_MSG);
            }
            SysModel sysModel = SysModel.getInstance(requestArgs.getModelCode());
            if (sysModel == null) {
                throw new BusinessException(ErrorCode.DB_RECORD_NOFIND,
                        StringUtil.format(ErrorConstant.DB_RECORD_NOFIND_MSG, requestArgs.getModelCode()));
            }
            IModel model = deserialization(sysModel, requestArgs);
            SysBaseRepository repository = ModelRepositoryFactory.builder(sysModel);
            repository.setBaseAttrVal(model, userCredentials);
            Method method = this.getMethodByName(repository.getClass(), "delete", IModel.class, ITableInfo.class);
            this.execRepository(repository, method, model, sysModel);
            if(model.getObjectId() != null) {

                MessageSender messageSender = MessageSender.getMessageSender();
                if(messageSender != null) {
                    DataMessage dataMessage = DataMessage.valueOf(sysModel.getModelCode(), DataMessage.DELETE, model.getObjectId().toString(),
                            null, model, requestArgs.getSysCode(), userCredentials.getName());
                    messageSender.sendMsg(CommandRunTimeArgs.getRunTimeArgs().getDataQueueName(), dataMessage);
                }
            }
            return "OK";
        } catch (BusinessException businessException) {
            throw businessException;
        } catch (DalFrameException exception) {
            throw new BusinessException(exception.getCode(), exception.getMessage());
        } catch (Exception exception) {
            throw new BusinessException(ErrorCode.INTERNAL_SERVER_ERROR, exception.getMessage());
        }
    }

    @Override
    public String getBeanName() {
        return "DeleteModel";
    }
}
